/**
 *
 *
 * @author TOTORO
 * @date 2021/7/16 8:29
 */
var $lastCart = jQuery;
$(function () {
    // 悬浮在购物车上，获得最后一条购物车记录
    $("#menu-bd").on("mouseenter", ".sn-cart", function () {
        $lastCart = $(".mini-cart-bd").children(":last-child");
    });
    // 删除导航栏购物车里的商品
    $("#menu-bd").on("click", ".mini-cart-del", function () {
        $lastCart = $(this).parent().parent().children(":last-child");
        var $ul = $(this).parent().parent();
        var cpLength = $(this).parent().parent().children().length;
        // 获取点击的购物车id
        var cartId = $lastCart.attr("data-cartid");
        $(this).children().text("正在删除中");
        var that = $(this);
        setTimeout(function () {
            that.parent().remove();
            $.post("/cart_logic/deleteCartProduct.htm", {"cartId":cartId});
            // new cart
            if(cpLength >=5 ){
                $.post("/cart_logic/getNextCp.htm", {"cartId": cartId}, function (data) {
                    if(data!="no"){
                        var json = eval("("+ data +")");
                        console.log(json);
                        var $cp = $("<li data-cartid='"+json['cartId']+"'>\n" +
                            "<div class='mini-cart-img'>\n" +
                            "<a target='_top' href='//item.taobao.com/item.htm?id=600495300170'>\n" +
                            "<img src='"+json['productImages'][0]['imagePath']+"' alt='"+json['productName']+"'>\n" +
                            "</a>\n" +
                            "</div>\n" +
                            "<div class='mini-cart-count'>¥<strong class='mini-cart-price h'>￥"+json['productSpecs']['productPrice']+"</strong>\n" +
                            "</div>\n" +
                            "<div class='mini-cart-title'><a target='_top' href='//item.taobao.com/item.htm?id=600495300170' title='"+json['productName']+"' style='width: 155px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;'>"+json['productName']+"</a>\n" +
                            "</div>\n" +
                            "<div class='mini-cart-del'><a href='#'>删除</a></div>\n" +
                            "<div class='mini-cart-info'>\n" +

                            "</div>\n" +
                            "</li>").appendTo($ul);
                        // 格式化sku
                        var skuData = json['productSpecs']['productSpaceData'];
                        var skuDatas = skuData.replaceAll("{","").replaceAll("}","").replaceAll("\"", "").split(",");
                        for (let i = 0; i < skuDatas.length; i++) {
                            var $skuTips = $("<span>"+skuDatas[i]+"</span>").appendTo($cp.find(".mini-cart-info"));
                        }
                    }
                })
            }
        }, 500);
    })
})